package cn.liaoc.journalismapi1.controller;

import cn.hutool.json.JSONUtil;
import cn.liaoc.journalismapi1.entity.Journalism;
import cn.liaoc.journalismapi1.service.JournalismClassifyService;
import cn.liaoc.journalismapi1.service.JournalismService;
import cn.liaoc.journalismapi1.utils.ResponseResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

@CrossOrigin("*")
@RestController
@RequestMapping("/api/jl")
public class JournalismController {

    @Resource
    JournalismService journalismService;

    @Resource
    JournalismClassifyService journalismClassifyService;

    Journalism journalism = new Journalism();

    @GetMapping("/")
    public String getJournalism(@RequestParam(required = false, defaultValue = "0") Integer classify, @RequestParam(defaultValue = "1", required = false) Integer pageNo) {
        QueryWrapper<Journalism> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(!(classify == 0),"j.classify", classify);
        Page<Journalism> journalismPage = new Page<>(pageNo, 3);
        IPage<Journalism> journalismIPage = journalismService.selectPage(journalismPage, queryWrapper);
        return JSONUtil.toJsonStr(new ResponseResult(200, "查询成功", journalismIPage));

    }

    @PostMapping("/add")
    public String addJournalism(@RequestParam Integer state ,@RequestParam String title, @RequestParam String author, @RequestParam Integer classify) {
        journalism.setAuthor(author);
        journalism.setTitle(title);
        journalism.setClassify(classify);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        journalism.setReleaseTime(simpleDateFormat.format(new Date()));
        journalism.setState(state);
        journalismService.save(journalism);
        return JSONUtil.toJsonStr(new ResponseResult(200, "添加成功", null));
    }

    @PutMapping("/update")
    public String updateJournalism(@RequestParam Integer id, @RequestParam Integer state ,@RequestParam String title, @RequestParam String author, @RequestParam Integer classify) {
        journalism.setId(id);
        journalism.setAuthor(author);
        journalism.setTitle(title);
        journalism.setClassify(classify);
        journalism.setState(state);
        journalismService.updateById(journalism);
        return JSONUtil.toJsonStr(new ResponseResult(200, "修改成功", null));
    }

    @DeleteMapping("/delete")
    public String deleteJournalism(@RequestParam Integer id) {
        journalismService.removeById(id);
        return JSONUtil.toJsonStr(new ResponseResult(200, "删除成功", null));
    }

    @GetMapping("/classify")
    public String getClassify() {
        return JSONUtil.toJsonStr(new ResponseResult(200, "查询成功", journalismClassifyService.list()));
    }

    @GetMapping("/updateList")
    public String getUpdateList(@RequestParam Integer id) {
        return JSONUtil.toJsonStr(new ResponseResult(200, "查询成功", journalismService.getById(id)));
    }
}
